﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;

namespace IMEI计算器
{
	public partial class FormImeiCalc : Form
	{
		public FormImeiCalc()
		{
			InitializeComponent();
		}

		private void btnPaste_Click(object sender, EventArgs e)
		{
			txtImei.Text = Clipboard.GetText();
		}

		private void txtImei_TextChanged(object sender, EventArgs e)
		{
			String imei = txtImei.Text;
			if (imei.Length != 15)
			{
				txtGivenChecksum.Text = "";
				txtGivenChecksum.BackColor = SystemColors.Control;
			}

			if (imei.Length == 15)
			{
				txtGivenChecksum.Text = imei.Substring(14, 1);
				txtChecksum.Text = CalcImeiChecksum(imei.Substring(0, 14));
				txtGivenChecksum.BackColor = txtChecksum.Text == txtGivenChecksum.Text ? Color.Green : Color.Red;
			}
			else if (imei.Length == 14)
			{
				txtChecksum.Text = CalcImeiChecksum(imei.Substring(0, 14));
			}
			else
			{
				txtChecksum.Text = "";
			}
		}

		public static string CalcImeiChecksum(string imei)
		{
			string checksum = "";
			if (imei.Length == 14)
			{
				//char[] imeiChar = imei.ToArray();
				int resultInt = 0;
				for (int i = 0; i < imei.Length; i++)
				{
					int a = int.Parse(imei.Substring(i, 1));
					if (i % 2 == 1)
					{
						int t = 2 * a;
						a = (t / 10) + (t % 10);
					}
					resultInt += a;
				}
				resultInt %= 10;
				resultInt = resultInt == 0 ? 0 : 10 - resultInt;
				checksum = resultInt.ToString();
			}

			return checksum;
		}

		private void FormImeiCalc_Load(object sender, EventArgs e)
		{
			//txtImei.Text = "";
			txtImei_TextChanged(sender, e);
		}

		private void btnRangeOutFname_Click(object sender, EventArgs e)
		{
			saveFileDialog_rangeOut.FileName = txtRangeOutFname.Text;
			if (saveFileDialog_rangeOut.ShowDialog() == DialogResult.OK)
			{
				txtRangeOutFname.Text = saveFileDialog_rangeOut.FileName;
			}
		}

		private void btnImeiListFileIn_Click(object sender, EventArgs e)
		{
			openFileDialog_listFileIn.FileName = txtImeiListFileIn.Text;
			if (openFileDialog_listFileIn.ShowDialog() == DialogResult.OK)
			{
				txtImeiListFileIn.Text = openFileDialog_listFileIn.FileName;
			}

		}

		private void btnImeiListFileOut_Click(object sender, EventArgs e)
		{
			saveFileDialog_listFileOut.FileName = txtImeiListFileOut.Text;
			if (saveFileDialog_listFileOut.ShowDialog() == DialogResult.OK)
			{
				txtImeiListFileOut.Text = saveFileDialog_listFileOut.FileName;
			}

		}

		public static bool IsDigital(string str)
		{
			Regex reg = new Regex("^[0-9]+$");
			Match ma = reg.Match(str);
			bool result = ma.Success;
			return result;
		}

		private void btnCalcRange_Click(object sender, EventArgs e)
		{
			if ((txtImeiBegin.Text.Length != 14 || txtImeiEnd.Text.Length != 14) ||
				(!IsDigital(txtImeiBegin.Text) || !IsDigital(txtImeiEnd.Text)))
			{
				MessageBox.Show("IMEI必须是14位全数字");
				return;
			}

			if (txtImeiBegin.Text.Substring(0, 8) != txtImeiEnd.Text.Substring(0, 8))
			{
				MessageBox.Show("IMEI范围不正确（例如：超过百万）");
				return;
			}

			string tac_fac = txtImeiBegin.Text.Substring(0, 8);
			int productSnBegin = int.Parse(txtImeiBegin.Text.Substring(8));
			int productSnEnd = int.Parse(txtImeiEnd.Text.Substring(8));

			using (StreamWriter sw = new StreamWriter(txtRangeOutFname.Text))
			{
				for (int productSn = productSnBegin; productSn <= productSnEnd; productSn++)
				{
					string imei = tac_fac + String.Format("{0:000000}", productSn);
					string checksum = CalcImeiChecksum(imei);
					sw.WriteLine(imei + checksum);
				}
			}
		}

		private void btnCalcByFile_Click(object sender, EventArgs e)
		{
			using (StreamReader sr = new StreamReader(txtImeiListFileIn.Text))
			using (StreamWriter sw = new StreamWriter(txtImeiListFileOut.Text))
			{
				while (true)
				{
					string imei = sr.ReadLine();
					if (imei == null)
						break;

					if (String.IsNullOrEmpty(imei))
						continue;

					string output;
					if ((imei.Length != 14 && imei.Length != 15) || !IsDigital(imei))
					{
						output = String.Format("-- IMEI必须是14/15位全数字 {{{0}}}", imei);
					}
					else
					{
						string checksum = CalcImeiChecksum(imei);
						output = imei + checksum;
					}
					sw.WriteLine(output);
				}
			}
		}
	}
}
